// package-market/components/lottery-type-wheel/index.js
import componentBehaviors from '../../../behaviors/component';
import { COMPONENT_OPTIONS, NUMERAL_ZH } from '../../../constants/common';
import { PACKAGE_MARKET_PAGE } from '../../../constants/route';
import { navTo } from '../../../common/env';
import { isEmpty } from '../../../common/utils';
import { dealWheelLotteryAwardData, getOneEmptyValue } from '../../biztools/lottery';

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        // 活动id
        activityId: {
            type: String,
            value: '',
        },
        // 通知列表
        noticeList: {
            type: Array,
            value: [],
        },
        // 奖品列表
        awardList: {
            type: Array,
            value: [],
        },
        // 抽奖机会
        count: {
            type: Number,
            value: 0,
        },
        // 抽奖结果
        result: {
            type: Object,
            value: {},
        },
    },
    observers: {
        'result': function (result) {
            if (result && !isEmpty(result)) {
                const { prize } = result;
                const { awardList, showList } = this.data;
                let { index } = getOneEmptyValue();
                if (prize > 0) {
                    index = showList.findIndex(i => i.prize === prize);
                }
                this.animateAction(index);
            }
        },
        'awardList': function(list) {
            if (list && !isEmpty(list)) {
                this.setData({
                    showList: dealWheelLotteryAwardData(list),
                });
            }
        },
    },
    data: {
        NUMERAL_ZH,
        animateDeg: 0,
        showList: [],
        showResult: false,
        lotteryResult: {},
    },
    methods: {
        // 开始抽奖
        handleStart(e) {
            this.triggerEvent('start');
        },
        // 动画
        animateAction: function (endIndex) {
            const { showList } = this.data;
            const animateBaseDeg = [0, 315, 270, 225, 180, 135, 90, 45];
            const award = showList.filter(item => item.value == endIndex + 1)[0];
            // 中间匀速，10圈
            const centerAngle = 7 * 360;
            // 结束最后的转动结果，多转一圈
            const endAngle = animateBaseDeg[endIndex] + 3 * 360;
            // 总共
            const totalAngle = centerAngle + endAngle;
            // 最大速度啊
            let maxSpeed = 16;
            let speed = 0.1;
            // 转动角度
            let animateDeg = 0;
            // 每秒 60 帧
            this.wheelTimer = setInterval(() => {
                // 开始
                if (animateDeg <  totalAngle - 1304) {
                    animateDeg += speed;
                    if (speed < maxSpeed) {
                        speed += 0.1;
                    } else {
                        speed = maxSpeed;
                    }
                } else {
                    animateDeg += speed;
                    if (speed > 0.1) {
                        speed -= 0.1;
                    } else {
                        speed = 0.1;
                    }
                    if (animateDeg >= (totalAngle)) {
                        animateDeg = totalAngle;
                        clearInterval(this.wheelTimer);
                        this.setData({
                            lotteryResult: award,
                            showResult: true,
                        });
                    }
                }
                this.setData({
                    animateDeg,
                });
            }, 1000 / 60);
        },
        // 关闭
        handleClose(e) {
            if (e.detail) {
                const { activityId } = this.data;
                navTo(PACKAGE_MARKET_PAGE.lotteryRecord, {
                    activityId,
                });
            } else {
                this.setData({
                    showResult: false,
                });
            }
        },
        // 分享
        handleShare() {
            this.triggerEvent('share');
        },
    }
})
